package org.spiderflow.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spiderflow.core.model.FlowNotice;
import org.spiderflow.core.service.FlowNoticeService;
import org.spiderflow.common.enums.FlowNoticeWay;
import org.spiderflow.common.model.JsonBean;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/spider/flowNotice")
public class FlowNoticeController {

    private static final Logger logger = LoggerFactory.getLogger(FlowNoticeController.class);
    private final FlowNoticeService flowNoticeService;

    public FlowNoticeController(FlowNoticeService flowNoticeService) {
        this.flowNoticeService = flowNoticeService;
    }

    @RequestMapping("/save")
    public JsonBean save(FlowNotice entity) {
        if (StringUtils.isEmpty(entity.getId())) {
            return new JsonBean(0, "流程id不能为空");
        }
        try {
            flowNoticeService.saveOrUpdate(entity);
        } catch (RuntimeException e) {
            logger.error(e.getMessage(), e);
            return new JsonBean(0, e.getMessage() == null ? "发生错误" : e.getMessage());
        }
        return new JsonBean(entity);
	}

    @RequestMapping("/find")
    public JsonBean find(String id) {
        FlowNotice data = flowNoticeService.getById(id);
        if (data == null) {
            data = new FlowNotice();
            data.setId(id);
        }
        return new JsonBean(data);
    }

    @RequestMapping("/getNoticeWay")
    public JsonBean getNoticeWay(String id) {
        return new JsonBean(FlowNoticeWay.getMap());
    }

}
